<?php

class VacPhotoUpload extends DataObject implements PermissionProvider
{
    private static $singular_name = "Camera Upload";

    private static $plural_name = "Camera Uploads";

    private static $db = array(
        "Message" => "Text(4000)",
        "PhotoStatus" => "Enum(array('unread', 'read'))"
    );

    private static $has_one = array(
        "Resident" => "Resident",
        "PhotoFile" => "File"
    );

    private static $searchable_fields = array(
        "PhotoStatus" => array("title" => "Status"),
        "Created" => array("title" => "Uploaded At")
    );

    private static $summary_fields = array(
        "Resident.Username",
        "Created.Nice24",
        "FormattedPhotoStatus",
        "PhotoFileThumb"
    );

    private static $default_sort = "PhotoStatus, ID DESC";

    private static $field_labels = array(
        "Resident.Username" => "Username",
        "FormattedPhotoStatus" => "Status",
        "Created.Nice24" => "Uploaded At",
        "PhotoFileThumb" => "Preview"
    );

    public function canView($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "VIEW_VacPhotoUpload"
        ), "any", $member);
    }

    public function canEdit($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "EDIT_VacPhotoUpload"
        ), "any", $member);
    }

    public function canDelete($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "DELETE_VacPhotoUpload"
        ), "any", $member);
    }

    public function canCreate($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "CREATE_VacPhotoUpload"
        ), "any", $member);
    }

    public function FormattedPhotoStatus()
    {
        if ($this->PhotoStatus == "read") {
            return "Read";
        } else {
            return "New";
        }
    }

    function PhotoFileThumb(){
        if ($this->PhotoFile()) {
            return new LiteralField("", '<img alt="' . $this->Title . '" src="' . $this->PhotoFile()->getAbsoluteURL() . '" style="width: 40px; border: 1px #b3b3b3 solid;" />');
        } else {
            return "N/A";
        }
    }

    public function getDefaultSearchContext()
    {
        $context = parent::GetDefaultSearchContext();

        $context->removeFieldByName("PhotoStatus");
        $context->addField($this->getPhotoStatusField(true));

        return $context;
    }

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->removeByName("PhotoStatus");

        if ($this->isInDB()) {
            if ($this->PhotoStatus != "read") {
                // Auto set status to read.
                $this->PhotoStatus = "read";
                $this->write();
            }

            if ($this->PhotoFile()) {
                $photoFilePath = $this->PhotoFile()->getAbsoluteURL();
                $photoPreviewField = new LiteralField("PhotoPreview", '<div class="field text"><label class="left">Photo Preview</label><br/><img alt="" src="' . $photoFilePath . '" style="border: 1px #b3b3b3 solid; max-width: 800px;" /></div>');

                $fields->insertAfter($photoPreviewField, "PhotoFile");
            }
        }

        if ($this->canEdit() || $this->canCreate()) {
            $uploadField = new UploadField("PhotoFile", "Upload Photo");
            $uploadField->setAllowedExtensions(array("jpg", "jpeg", "png", "gif", "bmp"));
            $uploadField->setFolderName("Uploads/resident_camera/");

            $fields->replaceField("PhotoFile", $uploadField);
        } else {
            $fields->removeByName("PhotoFile");
        }

        return $fields;
    }

    public function getPhotoStatusField($showAny = false)
    {
        $arr = array(
            "" => "(Any)",
            "read" => "Read",
            "unread" => "New"
        );

        if (!$showAny) {
            unset($arr[""]);
        }

        $field = new DropdownField
        (
            "PhotoStatus",
            _t("General.PHOTO_STATUS", "Status"),
            $arr
        );

        return $field;
    }

    public function providePermissions()
    {
        $category = "Resident support permissions";

        return array(
            "VIEW_VacPhotoUpload" => array(
                "name" => "View all records",
                "category" => $category
            ),
            "CREATE_VacPhotoUpload" => array(
                "name" => "Create new record",
                "category" => $category
            ),
            "EDIT_VacPhotoUpload" => array(
                "name" => "Edit record",
                "category" => $category
            ),
            "DELETE_VacPhotoUpload" => array(
                "name" => "Delete record",
                "category" => $category
            )
        );
    }
}